/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.business.bean.AutoReleaseBean;
import com.rf.business.dao.AutoReleaseMapper;
import com.rf.business.model.AutoRelease;
import com.rf.business.model.AutoReleaseExample;
import com.rf.business.service.AutoReleaseService;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.Department;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.utils.PageUtils;

@Service("autoReleaseService")
public class AutoReleaseServiceImpl implements AutoReleaseService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private AutoReleaseMapper autoReleaseMapper;//注入dao

	@Resource
	private DepartmentService departmentService;
   	/**
	 * 根据id获取数据
	 * @param autoReleaseId
	 * @return AutoRelease
	 */
   	@Override
	public AutoRelease getAutoReleaseByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return autoReleaseMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据AutoRelease获取数据
	 * @param AutoRelease
	 * @return List<AutoRelease>
	 */
	@Override
	public List<AutoRelease> getAutoRelease(AutoRelease autoRelease) throws Exception{
		// TODO Auto-generated method stub
		AutoReleaseExample example = new AutoReleaseExample();
		com.rf.business.model.AutoReleaseExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<AutoRelease> list = autoReleaseMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<AutoRelease>
	 */
	@Override
	public List<AutoRelease> getAutoRelease() throws Exception{
		// TODO Auto-generated method stub
		AutoReleaseExample example = new AutoReleaseExample();
		com.rf.business.model.AutoReleaseExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return autoReleaseMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param AutoRelease
	 * @return
	 */
	@Override
	public void addAutoRelease(AutoRelease autoRelease) throws Exception{
		// TODO Auto-generated method stub
		autoReleaseMapper.insert(autoRelease);
	}

	/**
	 * 修改数据
	 * @param AutoRelease
	 * @param
	 */
	@Override
	public void updateAutoRelease(AutoRelease autoRelease) throws Exception{
		// TODO Auto-generated method stub
		autoReleaseMapper.updateByPrimaryKeySelective(autoRelease);
	}
	
	/**
	 * 删除数据
	 * @param autoReleaseId
	 * @param
	 */
	@Override
	public void deleteAutoRelease(String id
) throws Exception{
		// TODO Auto-generated method stub
		autoReleaseMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param AutoRelease
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getAutoReleasePage(AutoRelease autoRelease,PageUtils pageUtils) throws Exception{
		AutoReleaseExample example = new AutoReleaseExample();
		com.rf.business.model.AutoReleaseExample.Criteria criteria= example.createCriteria();
		if(autoRelease.getEntryExitType() != null && !autoRelease.getEntryExitType().equals("")){
			criteria.andEntryExitTypeEqualTo(autoRelease.getEntryExitType());
		}
		if(autoRelease.getIsAuto() != null && !autoRelease.getIsAuto().equals("")){
			criteria.andIsAutoEqualTo(autoRelease.getIsAuto());
		}
		Session session = SecurityUtils.getSubject().getSession();
		boolean isAdmin = (Boolean)session.getAttribute("isAdmin");
		if(!isAdmin){
			criteria.andDepIdEqualTo((String)session.getAttribute("depSessionId"));
		}
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<AutoRelease> list = autoReleaseMapper.selectByExample(example);
		List<AutoReleaseBean> listBean = new ArrayList<AutoReleaseBean>();
		for(AutoRelease a :list){
			Department dep = departmentService.getDepartmentByID(a.getDepId());
			AutoReleaseBean aB = new AutoReleaseBean();
			aB.setDepId(a.getDepId());
			aB.setDepName(dep.getName());
			aB.setEntryExitType(a.getEntryExitType());
			aB.setId(a.getId());
			aB.setIsAuto(a.getIsAuto());
			listBean.add(aB);
		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", listBean);
		return dateMap;
	}
}

